<?php

namespace app\admin\model\goods;
use think\Model;

class Cart extends Model
{

    // 表名
    protected $name = 'cart';
    // 自动写入时间戳字段
//    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'ctime';
    // 追加属性
    protected $append = [

    ];

    public function getOriginData()
    {
        return $this->origin;
    }

    protected static function init()
    {
    }

    /**
     * @Explain :添加购物车
     * @param $uid
     * @param $goodsId
     * @param $quantity
     * @Date : 2020/9/4 16:50
     * @Author : 河南邦耀网络科技有限公司
     */

    public function add($uid,$goodsId,$quantity=1){
        $goodsInfo=Goods::get(['goodsid'=>$goodsId]);
        if(!$goodsInfo) throw new Exception('商品不存在',0);
        $goods=Cart::get(['uid'=>$uid,'goodsId'=>$goodsId]);
        if(!empty($goods)){
            return Cart::update(['quantity'=>$goods['quantity']+$quantity],['uid'=>$uid,'goodsId'=>$goodsId]);
        }
        $data=[
            'uid'=>$uid,
            'goodsId'=>$goodsId,
            'quantity'=>$quantity,
            'category'=>$goodsInfo->category,
            'selfCateId'=>$goodsInfo->SelfCateId,
            'collectPrice'=>$goodsInfo->price,
            'ctime'=>time()
        ];
       return Cart::create($data);
    }
}
